<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%@ taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt" %>
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>支付表结清数据</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
<div class="Wrapper">
	<div id="pageInTab" class="pageInTab">
	            <form id="formSet1">
	                <div class="fieldset1 tabel_col clearFix">
	                     <div class="fieldBox col_50">
		                    <div class="label">结清金额</div>
		                    <div class="field">
		                   <input name="stlAmt" id="stlAmt" type="text" class="ui-textbox"   value="" >
		                     <span class="sq-button sq-button-round"  onclick="modifyStlAmt()">修改</span>
		                    </div>
		                </div>
		                 <div class="fieldBox col_50">
		                    <div class="label">支付表状态</div>
		                    <div class="field">
       		                  <select name="prdStsCd"  id="prdStsCd" >
			                       <option value="">--请选择--</option>
			                         <c:forEach items="${prdStsCdList}" var="opt">
			                          <option value="${opt.key}">${opt.value}</option>
			                         </c:forEach>
                   			  </select>
        		              
		                    </div>
		                </div>
		                <div class="fieldBox col_50">
		                    <div class="label">拖回时间</div>
		                    <div class="field">
		                   <input name="backDt" id="backDt" type="text"  class="ui-datepicker"  value="" >
		                    </div>
		                </div>
		                <div class="fieldBox col_50">
		                    <div class="label">结清时间</div>
		                    <div class="field">
		                   <input name="stlDt" id="stlDt" type="text"  class="ui-datepicker"  value="" >
		                   <span class="sq-button sq-button-round"  onclick="modifyPrdStsCd()">资产状态/时间修改</span>
		                    </div>
		                </div>
		                
		              <!--   <div class="fieldBox col_50">
		                    <div class="label">流程异常处理</div>
		                    <div class="field">
		                     <span >流程异常时插入流程中数据</span>
		                     <span class="sq-button sq-button-round"  onclick="dealExceptionData()">处理</span>
		                    </div>
		                </div> -->
		                  <div class="fieldBox col_100">
				            <div class="sqPanel">
				                <div id="detailGrid">
				                    <div id="ligergrid_def_fee_sett" class="sq-grid"></div>
				                </div>
				            </div>
				        </div>
				        
		                <!-- 添加结清费用项 -->
		              <div class="fieldBox col_100">
				            <div class="sqPanel">
				                <div class="sqPanel_head inL">
				                    <div class="btnsBox inLC">
				                        <span id="addBtn" class="sq-button sq-button-round"><!-- <i class="sqBtnIco_3"></i> -->添加</span>
				                        <span id="delBtn" class="sq-button sq-button-round"><!-- <i class="sqBtnIco_7"></i> -->删除</span>
				                    </div>
				                </div>
				                 <br/>
				                  <br/>
				                <div id="detailGrid">
				                    <div id="ligergrid_fee_sett" class="sq-grid"></div>
				                </div>
				            </div>
				        </div>
	                </div>
	                <div class="btnsBox">
		                <span id="saveBtn1" class="sq-button sq-button-round"><i class="sqBtnIco_10"></i>保存</span>
		            </div>
	            </form>
	</div>
</div>

<script type="text/javascript">
var id = '${id}';
var dtlId = '${dtlId}';
var feeSettGrid;//结清费用项
var subFeeAmt = '${subFeeAmtTotalList}';
if(subFeeAmt){
subFeeAmt = JsonUtils.parse(subFeeAmt);
}
var feeSettGrid;//结清费用项
var defFeeSettGrid;
var settAddFeeAmt=0; //结清添加的费用项
var prjId = '${prjId}';
//费用类型
var settFeeTypCdListJSON = [];
<c:if test="${not empty settFeeTypCdListJSON}"> settFeeTypCdListJSON = ${settFeeTypCdListJSON};</c:if>
var yesOrNoCdListJSON = [];
<c:if test="${not empty yesOrNoCdListJSON}"> yesOrNoCdListJSON = ${yesOrNoCdListJSON};</c:if>

$(function(){
	$('#formSet1').ligerForm({
        validate : true
	});
	var pay = '${pay}';
	var listPage = '${listPage}';
	var feeListPage = '${feeListPage}';
	
	
	
	prjQuatPrpsfeeSettGrid();
	prjQuatPrpsDefFeeSettGrid();

});

$("#addBtn").click(function (event) {
	feeSettGrid.addRow({
		feeTypCd: '',
		feeAmt:''
	});
}); 

$("#delBtn").click(function (event) {
var selectedRows = feeSettGrid.getSelectedRows();
	if(selectedRows.length <1){
		$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
		return;
	}
	var _ids = [];
	$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
		if(flag == false)return;
	for ( var index in selectedRows) {
		if(selectedRows[index].id){
		_ids.push(selectedRows[index].id);
		}
	}
	if(!_ids||_ids.length==0){
		var manager = $("#ligergrid_fee_sett").ligerGetGridManager();
		 //manager.deleteSelectedRow();
		 feeSettGrid.removeRange(feeSettGrid.getSelectedRows());
		 top.$.ligerDialog.SQalert({content: "删除成功！",SQ_type: "success"});
		 return;
	}
		if(flag == false)return;
		$.ajax({
			type: 'post',
			dataType:'json',
			data:{ids:JSON.stringify(_ids)},
			url: '<%=path%>/project/prjPrdPaySchD/removeByIds.do',
			success:function(msg){
				 if(msg.success == 1){
					 feeSettGrid.reload();
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
				 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
				 }
			}
		});
	});
	
});  



function prjQuatPrpsDefFeeSettGrid(){
	var  params = {};
	var id = '${id}';
	       params.paySchId = id;
	defFeeSettGrid = $("#ligergrid_def_fee_sett").ligerGrid({
	method : 'get',
	parms:params,
	url: '<%=path%>/project/prjPrdPaySchD/getPrjPrdPaySchDefSettFeeList.do',
    columns: [
        {display: '费用类型', name: 'feeTypCdNm', minWidth: 80},
        {display: '金额', name: 'feeAmt', minWidth: 80, type: 'numberbox',
        	render: function (item) {
       		 var amt = item.feeAmt;
       		 if(item.capFlow==ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT){
       			 amt = "<span style='color:red'>-"+item.feeAmt+"</span>";
       		 }
       		 if(item.codeAttr1==ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT){
       			 amt = "<span style='color:red'>-"+item.feeAmt+"</span>";
       		 }
                 return amt;
             }
        },
        getOptColumn()
        
    ],
    usePager: false,
    pageSize:100,
    root: 'rows',
    record: 'total',
    hideLoadButton: true,
    width: '100%',
    height: 300,
    heightDiff: -2,
    enabledEdit: false,
    isScroll: false,
    checkbox: true,
    rownumbers: true,
    allowUnSelectRow: true,
    alternatingRow: true,
    usePagerOnLessPagesize: false,
});
}

function getOptColumn() {
    var column = {
        display: '操作',
        isSort: false,
        width: 250,
        align: 'left',
        render: function (rowdata, rowindex, value) {
            var h = "";
            h += '<a class="sq-button-hasicon" href="javascript:removeBtn(';
            h += "'" + rowdata.id + "'";
            h += ')"><i class="sqBtnIco_7"></i>删除</a>'
            return h;
        }
    }
    return column;
}

//删除所选费用项
function removeBtn(id) {
	var _ids = [];
	$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
		if(flag == false)return;
		_ids.push(id);
		if(flag == false)return;
		$.ajax({
			type: 'post',
			dataType:'json',
			data:{ids:JSON.stringify(_ids)},
			url: '<%=path%>/project/prjPrdPaySchD/removeByIds.do',
			success:function(msg){
				 if(msg.success == 1){
					 defFeeSettGrid.reload();
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
				 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
				 }
			}
		});
	});
}
function prjQuatPrpsfeeSettGrid(){
	var  params = {};
	var id = '${id}';
	       params.paySchId = id;
	       params.prjId = prjId;
	feeSettGrid = $("#ligergrid_fee_sett").ligerGrid({
	method : 'get',
	parms:params,
	url: '<%=path%>/project/prjPrdPaySchD/getPrjPrdPaySchSettFeeList.do',
    columns: [
        {display: '费用类型', name: 'feeTypCd', minWidth: 80,
            editor: {
                type: 'select',
                data: settFeeTypCdListJSON,
                valueField: 'code',
                textField: 'codeNm'
            },
            valueField: 'feeTypCd',
            textField: 'feeTypCdNm',
            render: function (item) {
                return item.feeTypCdNm;
            }
        },
        {display: '金额', name: 'feeAmt', minWidth: 80, type: 'numberbox',
        	editor: {type: 'number'},
        	 render: function (item) {
        		 var amt = item.feeAmt;
        		 if(item.capFlow==ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT){
        			 amt = "<span style='color:red'>-"+item.feeAmt+"</span>";
        		 }
        		 if(item.codeAttr1==ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT){
        			 amt = "<span style='color:red'>-"+item.feeAmt+"</span>";
        		 }
                  return amt;
              }
        },
        {display: '是否默认', name: 'isSysDef', minWidth: 80,
            editor: {
                type: 'select',
                data: yesOrNoCdListJSON,
                valueField: 'key',
                textField: 'value'
            },
            valueField: 'isSysDef',
            textField: 'isSysDefNm',
            render: function (item) {
                return item.isSysDefNm;
            }
        }
     
        
    ],
	onLoaded:function(){
     	this.toggleLoading(false);
     },
    onBeforeSubmitEdit: function (e) {//重新赋值,要不然选中后离开会把key显示在前端
    	var colNanme = e.column.columnname;
        if (e.selected && e.column.columnname == "feeTypCd") {
            e.record.feeTypCdId = e.value;
            e.value = e.selected[e.column.editor.textField];
            e.record.codeAttr1 = e.selected.codeAttr1;
        }
        if (e.selected && e.column.columnname == "isSysDef") {
            e.record.isSysDefId = e.value;
            e.value = e.selected[e.column.editor.textField];
        }
    
    		if(e.record.isSysDef==1){
    		   e.column.editor.readonly = true;
		 	}else{
		 	    e.column.editor.readonly = false;
		 	}
    },
    onAfterEdit: function (e) {
       	if(e.column.name =="feeAmt"){
   		 var amt =e.value;
		 if(e.record.codeAttr1==ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT){
			 amt = "<span style='color:red'>-"+e.value+"</span>";
		 }
		 if(e.record.codeAttr1==ProjectConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT){
			 amt = "<span style='color:red'>"+e.value+"</span>";
		 }
		 feeSettGrid.updateCell('capFlow', e.record.codeAttr1, e.record);	 
     	} 
      },  
    usePager: false,
    pageSize:100,
    root: 'rows',
    record: 'total',
    hideLoadButton: true,
    width: '100%',
    height: 300,
    heightDiff: -2,
    enabledEdit: true,
    isScroll: false,
    checkbox: true,
    rownumbers: true,
    allowUnSelectRow: true,
    alternatingRow: true,
    usePagerOnLessPagesize: false,
});
}
//格式化为人民币格式
function renderAmt(rowdata, rowindex, value) {
	if(null!==value&&value!=='undefined'&&value!==""){ 
	if(typeof Number(value)=='number'){
    var h="￥" + FormatMoney((parseFloat(value)).toFixed(2));
    return h;
	}
	} 
}
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
		}
		return dateStr;
	}
	
	function modifyStlAmt(){
		var id = '${id}';
		var stlAmt = $("#stlAmt").val();
		var data = {};
		data["id"] =id;
		data["stlAmt"] =stlAmt;
		  var url = '<%=path%>/project/prjPrdPaySchD/modifySettAmt.do';
		    var wait_dialog;
		    $.ajax({
				type: 'post',
				dataType:'json',
				data:data,
				url: url,
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					 wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:"修改成功！", SQ_type: "success"});
					 }else{
						 $.ligerDialog.SQalert({content:"修改失败！", SQ_type: "error"});
					 }
				}
			});
	}
	
	function modifyPrdStsCd(){
		var dtlId = '${dtlId}';
		var prdStsCd = $("#prdStsCd").val();
		var stlDt = $("#stlDt").val();
		var backDt = $("#backDt").val();
		var data = {};
		data["id"] =dtlId;
		data["prdStsCd"] =prdStsCd;
		data["stlDt"] = stlDt;
		data["backDt"] = backDt;
		data["paySchId"] = id;
		  var url = '<%=path%>/project/prjprddtl/modifyPrjPrdDtl.do';
		    var wait_dialog;
		    $.ajax({
				type: 'post',
				dataType:'json',
				data:data,
				url: url,
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					 wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:"修改成功！", SQ_type: "success"});
					 }else{
						 $.ligerDialog.SQalert({content:"修改失败！", SQ_type: "error"});
					 }
				}
			});
	}
	function dealExceptionData(){
		var id = '${id}';
		var data = {};
		data["id"] =id;
		  var url = '<%=path%>/project/prjPrdPaySchM/dealExceptionData.do';
		    var wait_dialog;
		    $.ajax({
				type: 'post',
				dataType:'json',
				data:data,
				url: url,
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					 wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:"处理成功！", SQ_type: "success"});
					 }else{
						 $.ligerDialog.SQalert({content:"处理失败！", SQ_type: "error"});
					 }
				}
			});
	}
	
	$("#saveBtn1").click(function(event) {
		//验证是否有重复的费用项
		 var settDetailListData = feeSettGrid.getData();
		var settDetailListDataArr = [];
		 var arr = [];
		var  result=new Array();
		var	len=settDetailListData.length;
		var  result=new Array();
		 	if(settDetailListData){
		          for ( var i=0; i<settDetailListData.length;i++) {
		        	  var obj = settDetailListData[i];
		        	   if(!obj.capFlow){
		        		  obj.capFlow = obj.codeAttr1;
		        	  } 
		        	  if(obj.feeTypCdId){
		        	  obj.feeTypCd=obj.feeTypCdId;
		        	  }
		        	  if(obj.isSysDef){
			        	  obj.isSysDef=obj.isSysDefId;
			        	  }
		        	  settDetailListDataArr.push(obj);
			      		if(!obj.isSysDef||obj.isSysDef==CommonConstant.C_YN_N){
			      		  var idx = obj.feeTypCd;
			              if(result[idx]){
			                  result[idx] = parseInt(result[idx]) + 1;
			              }else{
			                  result[idx] = 1;
			              }
			    		}
				  }
		 	var data = {};
		 	var id = '${id}';
		 	data.paySchId = id;
		   	data["settFeeList"] = JSON.stringify(settDetailListDataArr);
	    var url = '<%=path%>/project/prjPrdPaySchD/addPrjPrdPaySettFee.do';
	    var wait_dialog;
	    $.ajax({
			type: 'post',
			dataType:'json',
			data:data,
			url: url,
			beforeSend: function(){
		          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
		     },
			success:function(msg){
				 wait_dialog.close();
				 if(msg.success == 1){
					 $.ligerDialog.SQalert({content:"保存成功！", SQ_type: "success"});
				 }else{
					 $.ligerDialog.SQalert({content:"保存失败！", SQ_type: "error"});
				 }
			}
		});
		 }
		});
	
</script>



</body>
</html>
